<?
require 'global.php';
include(ROOT."common/mail_send.php");
error_reporting(0);
$email_template=new DB(Temail_template,$conn);
// $email='three1415@126.com';
// $arr=array(
// 	'to'=>array($email),
// 	'subject'=>'Test email',
// 	'body'=>'This is a test',
// 	'host'=>EMAIL_HOST,
// 	'account'=>EMAIL_ACCOUNT,
// 	'password'=>EMAIL_PASSWORD,
// 	'port'=>EMAIL_PORT,
// 	'name'=>EMAIL_NAME,
// );
// //pre($arr);
// pre(mail_send($arr)); 
// $flag=1;

// exit;

// 返回码
$k=0;

$email=''; if(isset($_POST["email"]) && $_POST["email"]!="") $email=clean($_POST["email"]);
$key=''; if(isset($_POST["key"]) && $_POST["key"]!="") $key=clean($_POST["key"]);
if($key != ''){ // 通过验证 verify now
	$code=sha1($email.date('d'));
	$code=substr($code, 0,6);
	$code=strtoupper($code);
	$key=strtoupper($key);
	if($code==$key){
		$arr=array('is_verified'=>'1');
		if($customer->update($arr,'email',$email)) $k=1;
	}
}
else { // 发送验证邮件 send code
	$code=sha1($email.date('d'));
	$code=substr($code, 0,6);
	$ver_code=strtoupper($code);

	// 邮件变量 email variables
	$var_email=array(
		'%nickname%'=>$customer_nickname,
		'%date%'=>date('Y-m-d'),
		'%datetime%'=>date('Y-m-d H:i:s'),
		'%url%'=>$baseurl,
		'%code%'=>$ver_code,
		'%company%'=>$config['SITE_COMPANY_'.LANG],
	);
	// replace vars 替换变量
	$arr_email = $email_template->get_row(2); // id=2 模板
	$subject=$arr_email['subject_'.$lang];
	$body=$arr_email['content_'.$lang];
	foreach ($var_email as $key => $value) {
		$subject=str_replace($key, $value, $subject);
		$body=str_replace($key, $value, $body);
	}

	$body='<!doctype html><html>
	<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>'.$subject.'</title></head>
	<body>'.$body.'</body></html>';
	$arr=array(
		'to'=>array($email),
		'subject'=>$subject,
		'body'=>$body,
		'host'=>EMAIL_HOST,
		'account'=>EMAIL_ACCOUNT,
		'password'=>EMAIL_PASSWORD,
		'port'=>EMAIL_PORT,
		'name'=>EMAIL_NAME,
		'ssl'=>EMAIL_SSL,
	);
	//pre($arr);
	if(mail_send($arr)) $k=1;
}

// return
echo $k;

